package com.ytsky.mybatis.demo;

import com.ytsky.mybatis.demo.dao.ProductMapper;
import com.ytsky.mybatis.demo.dao.ProductTypeMapper;
import com.ytsky.mybatis.demo.entity.Product;
import com.ytsky.mybatis.demo.entity.ProductType;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.util.List;

public class MyBatisSimpleDemo03 {
    private static SqlSessionFactory factory;
    static {
        try {
            factory=new SqlSessionFactoryBuilder()
                    .build(Resources.getResourceAsStream("mybatis-config.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
//        System.out.println(findProductsWithProductType());
        System.out.println(findProductTypesWithProducts());
    }
    private static List<Product> findProductsWithProductType(){
        SqlSession session = factory.openSession();
        List<Product> products = session.getMapper(ProductMapper.class).selectProductsWithProductType();
        session.close();
        return products;
    }
    private static List<ProductType> findProductTypesWithProducts(){
        SqlSession session = factory.openSession();
        List<ProductType> productTypes = session.getMapper(ProductTypeMapper.class).selectProductTypesWithProducts();
        session.close();
        return productTypes;
    }
}
